package com.da.javatest.thread.juc.future;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * @author chenlida
 * @date 2020/7/12 23:06
 *
 * 超时获取异步任务执行结果
 */
public class Future1 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(1);
        Future<Integer> result = executorService.submit(() -> {
            System.out.println(Thread.currentThread().getName() + ",start!");
            TimeUnit.SECONDS.sleep(3);
            System.out.println(Thread.currentThread().getName() + ",end!");
            return 10;
        });
        System.out.println(Thread.currentThread().getName());
        try {
            System.out.println(Thread.currentThread().getName() + ",结果：" + result.get(1, TimeUnit.SECONDS));
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        executorService.shutdown();
    }
}
